unit ProcuraCliente;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, Buttons, ExtCtrls, Grids, DBGrids, DB, DBTables,
  ComCtrls, ActnList, ImgList;

type
  TFormProcuraCliente = class(TForm)
    Panel2: TPanel;
    DataSource1: TDataSource;
    DBGridCliente: TDBGrid;
    QueryProcura: TQuery;
    ImageList2: TImageList;
    ActTotal: TActionList;
    ActionVoltar: TAction;
    ListView2: TListView;
    CheckBoxTodos: TCheckBox;
    Label1: TLabel;
    EditProcurar: TEdit;
    RadioGroupProcurar: TRadioGroup;
    procedure FormActivate(Sender: TObject);
    procedure DBGridClienteCellClick(Column: TColumn);
    procedure FormKeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    procedure ListView2Click(Sender: TObject);
    procedure ActionVoltarExecute(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure DataSource1DataChange(Sender: TObject; Field: TField);
    procedure DBGridClienteTitleClick(Column: TColumn);
    procedure CheckBoxTodosClick(Sender: TObject);
    procedure DBGridClienteDblClick(Sender: TObject);
    procedure EditProcurarKeyUp(Sender: TObject; var Key: Word;
      Shift: TShiftState);
  private
    { Private declarations }
    procedure ProcuraTexto;
  public
    { Public declarations }
  end;

var
  FormProcuraCliente: TFormProcuraCliente;
  vOrdem: string;

implementation

uses Principal, ProcuraProduto;

{$R *.dfm}

procedure TFormProcuraCliente.ProcuraTexto;
var sql:String;
begin

if RadioGroupProcurar.ItemIndex = 0 then
   //nome
   sql := 'select id,nm,telefone1,telefone2,telefone3,cliente_endereco(id) as endereco from cliente ' +
          'where left(nm,' + trim(IntToStr(length(EditProcurar.Text))) + ') = ''' + EditProcurar.Text + ''''
else if RadioGroupProcurar.ItemIndex = 1 then
        sql := 'select id,nm,telefone1,telefone2,telefone3,cliente_endereco(id) as endereco from cliente ' +
          'where left(endereco,' + trim(IntToStr(length(EditProcurar.Text))) + ') = ''' + EditProcurar.Text + ''''
else if RadioGroupProcurar.ItemIndex = 2 then
        sql := 'select id,nm,telefone1,telefone2,telefone3,cliente_endereco(id) as endereco from cliente ' +
          'where left(telefone1,' + trim(IntToStr(length(EditProcurar.Text))) + ') = ''' + EditProcurar.Text + ''' ' +
          'or left(telefone2,' + trim(IntToStr(length(EditProcurar.Text))) + ') = ''' + EditProcurar.Text + ''' ' +
          'or left(telefone3,' + trim(IntToStr(length(EditProcurar.Text))) + ') = ''' + EditProcurar.Text + '''';


//if EditTelefone.Text <> '' then
//   sql := 'select id,nm,telefone1,telefone2,telefone3,cliente_endereco(id) as endereco from cliente ' +
//          'where left(telefone1,' + trim(IntToStr(length(EditTelefone.Text))) + ') = ''' + EditTelefone.Text + ''' ' +
//          'or left(telefone2,' + trim(IntToStr(length(EditTelefone.Text))) + ') = ''' + EditTelefone.Text + ''' ' +
//          'or left(telefone3,' + trim(IntToStr(length(EditTelefone.Text))) + ') = ''' + EditTelefone.Text + ''''
//else
//   sql := 'select id,nm,telefone1,telefone2,telefone3,cliente_endereco(id) as endereco from cliente ' +
//          'where left(nm,' + trim(IntToStr(length(EditNome.Text))) + ') = ''' + EditNome.Text + '''';


//if not CheckBoxTodos.Checked then
//   sql := sql + ' and ativo = 1 ';

sql := sql + ' order by ' + vOrdem;
//showmessage(sql);

QueryProcura.SQL.Clear;
QueryProcura.SQL.Add(sql);
QueryProcura.Close;
QueryProcura.Open;

FormPrincipal.vProcTelefone := QueryProcura.fieldByName('telefone1').AsString;
       

       
end;

procedure TFormProcuraCliente.FormActivate(Sender: TObject);
begin
top := 0;
left :=0; 
QueryProcura.Open;

FormPrincipal.vProcTelefone := QueryProcura.fieldByName('telefone1').AsString;
FormPrincipal.vProcIDCliente := QueryProcura.fieldByName('id').AsInteger;

FormProcuraCliente.Caption := FormPrincipal.vTitulo + ' - Procurar Cliente';

DBGridCliente.SetFocus;

end;

procedure TFormProcuraCliente.DBGridClienteCellClick(Column: TColumn);
begin

FormPrincipal.vProcTelefone := QueryProcura.fieldByName('telefone1').AsString;
FormPrincipal.vProcIDCliente := QueryProcura.fieldByName('id').AsInteger;

end;

procedure TFormProcuraCliente.FormKeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
  if (key = VK_Escape) or (key = VK_Return) then
     self.Close;
end;

procedure TFormProcuraCliente.ListView2Click(Sender: TObject);
var List: TListView;
begin
if TListView(Sender).Selected <> nil then
  begin
        List := TlistView(Sender);
        ActTotal.Actions[List.Selected.StateIndex].Execute;
  end;

end;

procedure TFormProcuraCliente.ActionVoltarExecute(Sender: TObject);
begin
QueryProcura.Close;
close;
end;

procedure TFormProcuraCliente.FormShow(Sender: TObject);
begin

  //DBGridCliente.SetFocus;
  EditProcurar.SetFocus;
  vOrdem := 'telefone1';

  ProcuraTexto;
end;

procedure TFormProcuraCliente.DataSource1DataChange(Sender: TObject;
  Field: TField);
begin
   FormPrincipal.vProcTelefone := QueryProcura.fieldByName('telefone1').AsString;
   FormPrincipal.vProcIDCliente := QueryProcura.fieldByName('id').AsInteger;
end;

procedure TFormProcuraCliente.DBGridClienteTitleClick(Column: TColumn);
begin
vOrdem := Column.FieldName;
ProcuraTexto;
end;

procedure TFormProcuraCliente.CheckBoxTodosClick(Sender: TObject);
begin
ProcuraTexto;
end;

procedure TFormProcuraCliente.DBGridClienteDblClick(Sender: TObject);
begin
self.close;
end;

procedure TFormProcuraCliente.EditProcurarKeyUp(Sender: TObject;
  var Key: Word; Shift: TShiftState);
begin
ProcuraTexto;
end;

end.
